from pev2.data.DataModule import DataModule

"""
当日K线上穿和下穿10日均线的判断
"""

class DailyKBreakMA10Signal:

    def __init__(self):
        self.dm = DataModule()

    """
    判断某只股票在某日是否满足K线上穿10日均线

    :param code: 股票代码
    :param startDate: 开始日期
    :param endDate: 结束日期
    :return: True/False
    """
    def isKUpBreakMA10(self, code, startDate, endDate):
        # 如果没有指定开始日期和结束日期，则直接返回False
        if startDate is None or endDate is None:
            return False

        dailKS = self.dm.getKData(code, autype='hfq', startDate=startDate, endDate=endDate)

        # 需要判断两日的K线和10日均线的相对位置，所以如果K线数不满足11个，
        # 也就是无法计算两个MA10，则直接返回False
        indexSize = dailKS.index.size
        if indexSize < 11:
            return False

        # 比较收盘价和MA10的关系
        dailKS['ma'] = dailKS['close'].rolling(10).mean()
        dailKS['delta'] = dailKS['close'] - dailKS['ma']

        isBreakUp = (dailKS.loc[dailKS.index[9]]['delta'] <= 0 < dailKS.loc[dailKS.index[10]]['delta'])

        return isBreakUp

    """
    判断某只股票在某日是否满足K线下穿10日均线

    :param code: 股票代码
    :param beginDate: 开始日期
    :param endDate: 结束日期
    :return: True/False
    """
    def isKDownBreakMA10(self, code, startDate, endDate):

        # 如果没有指定开始日期和结束日期，则直接返回False
        if startDate is None or endDate is None:
            return False

        dailKS = self.dm.getKData(code, autype='hfq', startDate=startDate, endDate=endDate)

        # 需要判断两日的K线和10日均线的相对位置，所以如果K线数不满足11个，
        # 也就是无法计算两个MA10，则直接返回False
        if dailKS.index.size < 11:
            return False

        # 比较收盘价和MA10的关系
        dailKS['ma'] = dailKS['close'].rolling(10).mean()
        dailKS['delta'] = dailKS['close'] - dailKS['ma']

        return  dailKS.loc[dailKS.index[9]]['delta'] >= 0 > dailKS.loc[dailKS.index[10]]['delta']